import sys
import tarfile
import time

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders

def send_email():
    sender_email = '3041471187@qq.com'       # 发件人邮箱
    receiver_email = 'handong97@163.com' # 收件人邮箱
    password = 'kcxgomyuizbwdejc'     # QQ邮箱授权码（非密码！）
    smtp_server = 'smtp.qq.com'
    smtp_port = 465

    # 创建邮件内容
    msg = MIMEMultipart()
    msg['From'] = sender_email
    msg['To'] = receiver_email
    msg['Subject'] = '司秀丽的测试邮件'
    body = '这是一封司秀丽发送的测试邮件。'
    msg.attach(MIMEText(body, 'plain'))

    # 添加附件
    try:
        with open('a.tar.gz', 'rb') as attachment:
            part = MIMEBase('application', 'octet-stream')
            part.set_payload(attachment.read())
            encoders.encode_base64(part)
            part.add_header('Content-Disposition', 'attachment; filename="a.tar.gz"')
            msg.attach(part)
    except FileNotFoundError:
        print("错误：附件文件不存在！")
        return

    # 发送邮件
    server = None
    try:
        server = smtplib.SMTP_SSL(smtp_server, smtp_port)
        server.login(sender_email, password)
        server.sendmail(sender_email, receiver_email, msg.as_string())
        print("邮件发送成功！")
    except Exception as e:
        print(f"邮件发送失败: {e}")
    finally:
        if server is not None:
            server.quit()

if __name__ == '__main__':
    send_email()



# def  pack():
#     tar =  tarfile.open("a.tar.gz",mode="w:gz")
#     tar.add("report")
#
#
# def   upload(filename):
#     remote_dir = "/usr/local/nginx/html/allure"
#     username  = "root"
#     password = "123456"
#     hostname = "192.168.1.233"
#     client = paramiko.SSHClient()
#
#     # 设置自动添加到系统的主机列表中
#     client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
#     # 连接服务器
#     client.connect(hostname=hostname,username=username,password=password   ,port=22)
#
#     # 删除远程仓库的a.tar.gz报告文件
#     # client.exec_command(f"rm -rf  {remote_dir}/a.tar.gz")
#     # client.exec_command(f"rm -rf  {remote_dir}/*")
#     # 上传文件包数据
#     sftp = client.open_sftp()
#
#     #  上传
#     sftp.put("a.tar.gz",remote_dir+"/a.tar.gz")
#
#     # 等待时间
#     time.sleep(3)
#     print("上传成功！！！！")
#     # 解压
#     client.exec_command(f"cd  {remote_dir}")
#     time.sleep(1)
#     client.exec_command(f"tar -xvf   {remote_dir}/a.tar.gz -C  {remote_dir}")
#     time.sleep(2)
#
#     sftp.close()
#     client.close()
#
#
#
#
#
# if __name__ == '__main__':
#     pack()
#     upload("a.tar.gz")






















